ARD2  RC2
Airbag Reference Demonstrator using MPC5604P
freemaster_tsa.c
Go to the documentation of this file.
00001 /******************************************************************************
00002 *
00003 * Freescale Semiconductor Inc.
00004 * (c) Copyright 2004-2011 Freescale Semiconductor
00005 * ALL RIGHTS RESERVED.
00006 *
00007 ****************************************************************************/
00024 #include "freemaster.h"
00025 #include "freemaster_private.h"
00026 #include "freemaster_protocol.h"
00027 
00028 #if FMSTR_USE_TSA && (!FMSTR_DISABLE)
00029 
00030 /**************************************************************************/
00036 void FMSTR_InitTsa(void)
00037 {   
00038 }
00039 
00040 /**************************************************************************/
00051 FMSTR_BPTR FMSTR_GetTsaInfo(FMSTR_BPTR pMessageIO)
00052 {
00053     volatile FMSTR_BPTR pResponse = pMessageIO;
00054     const FMSTR_TSA_ENTRY* pTbl;
00055     FMSTR_TSA_TINDEX nTblIndex;
00056     FMSTR_TSA_TSIZE nTblSize = 0U;
00057     FMSTR_ADDR tmpAddr;
00058     FMSTR_U16 tmp16;
00059 
00060     /* get index of table the PC is requesting   */
00061     pMessageIO = FMSTR_SkipInBuffer(pMessageIO, 2U);
00062     pMessageIO = FMSTR_ValueFromBuffer16(&nTblIndex, pMessageIO);
00063     
00064     /* TSA flags */
00065     tmp16 = FMSTR_TSA_VERSION | FMSTR_TSA_FLAGS;
00066 
00067     /* sizeof TSA table entry items */
00068     /*lint -e{506,774} constant value boolean */
00069     if((sizeof(FMSTR_TSA_ENTRY)/4U) == 4U)
00070     {
00071         tmp16 |= FMSTR_TSA_INFO_32BIT;  /* 32bit TSA entries */
00072     }
00073     
00074     /* success, flags */
00075     pResponse = FMSTR_ConstToBuffer8(pResponse, FMSTR_STS_OK);
00076     pResponse = FMSTR_ValueToBuffer16(pResponse, tmp16);
00077 
00078     /* get the table (or NULL if no table on given index) */
00079     pTbl = FMSTR_TsaGetTable(nTblIndex, &nTblSize);
00080     FMSTR_PTR2ADDR(tmpAddr, pTbl);
00081 
00082     /* table size in bytes */
00083     nTblSize *= FMSTR_CFG_BUS_WIDTH;
00084     pResponse = FMSTR_ValueToBuffer16(pResponse, nTblSize);
00085     
00086     /* table address */
00087     return FMSTR_AddressToBuffer(pResponse, tmpAddr);
00088 }
00089 
00090 /**************************************************************************/
00101 FMSTR_BPTR FMSTR_GetStringLen(FMSTR_BPTR pMessageIO)
00102 {
00103     FMSTR_BPTR pResponse = pMessageIO;
00104     FMSTR_ADDR nAddr;
00105     FMSTR_U16 nLen = 0U;
00106     
00107     pMessageIO = FMSTR_SkipInBuffer(pMessageIO, 1U);
00108     pMessageIO = FMSTR_AddressFromBuffer(&nAddr, pMessageIO);
00109 
00110     nLen = FMSTR_StrLen(nAddr);
00111 
00112     /* return strign size in bytes (even on 16bit DSP) */
00113     nLen *= FMSTR_CFG_BUS_WIDTH ;
00114         
00115     pResponse = FMSTR_ConstToBuffer8(pResponse, FMSTR_STS_OK);
00116     return FMSTR_ValueToBuffer16(pResponse, nLen);
00117 }
00118 
00119 /**************************************************************************/
00133 #if defined(FMSTR_PLATFORM_56F8xxx) || defined(FMSTR_PLATFORM_56F8xx)
00134 /* make inline */
00135 inline FMSTR_BOOL FMSTR_CheckMemSpace(FMSTR_ADDR nAddrUser, FMSTR_SIZE8 nSizeUser, 
00136     FMSTR_ADDR nAddrSafe, FMSTR_SIZE wSizeSafe);
00137     
00138 inline
00139 #else
00140 /* declare function prototype */
00141 static FMSTR_BOOL FMSTR_CheckMemSpace(FMSTR_ADDR nAddrUser, FMSTR_SIZE8 nSizeUser, 
00142     FMSTR_ADDR nAddrSafe, FMSTR_SIZE wSizeSafe);
00143     
00144 static
00145 #endif
00146 
00147 FMSTR_BOOL FMSTR_CheckMemSpace(FMSTR_ADDR nAddrUser, FMSTR_SIZE8 nSizeUser, 
00148                                FMSTR_ADDR nAddrSafe, FMSTR_SIZE wSizeSafe)
00149 {
00150     FMSTR_BOOL bRet = FMSTR_FALSE;
00151     
00152     #ifdef __HCS12X__
00153     /* convert from logical to global if needed */
00154     nAddrUser = FMSTR_FixHcs12xAddr(nAddrUser);
00155     nAddrSafe = FMSTR_FixHcs12xAddr(nAddrSafe);
00156     #endif
00157 
00158     if(nAddrUser >= nAddrSafe)
00159     {
00160         bRet = (FMSTR_BOOL)
00161             (((nAddrUser + nSizeUser) <= (nAddrSafe + wSizeSafe)) ? FMSTR_TRUE : FMSTR_FALSE);
00162     }
00163     
00164     return bRet;
00165 }
00166 
00167 /**************************************************************************/
00179 FMSTR_BOOL FMSTR_CheckTsaSpace(FMSTR_ADDR dwAddr, FMSTR_SIZE8 nSize, FMSTR_BOOL bWriteAccess)
00180 {
00181     const FMSTR_TSA_ENTRY* pte;
00182     FMSTR_TSA_TINDEX nTableIndex;
00183     FMSTR_TSA_TSIZE i, cnt;
00184     FMSTR_SIZE nInfo;
00185 
00186 #if FMSTR_CFG_BUS_WIDTH >= 2U
00187     /* TSA tables use sizeof() operator which returns size in "bus-widths" (e.g. 56F8xx) */
00188     nSize = (nSize + 1) / FMSTR_CFG_BUS_WIDTH;
00189 #endif
00190     
00191     /* to be as fast as possible during normal operation, 
00192        check variable entries in all tables first */
00193     for(nTableIndex=0U; (pte=FMSTR_TsaGetTable(nTableIndex, &cnt)) != NULL; nTableIndex++)
00194     {
00195         /* number of items in a table */
00196         cnt /= (FMSTR_TSA_TSIZE) sizeof(FMSTR_TSA_ENTRY);
00197 
00198         /* all table entries */
00199         for(i=0U; i<cnt; i++)
00200         {
00201             /*lint -e{506,774} condition always true/false */
00202             if(sizeof(pte->addr.p) < sizeof(pte->addr.n))
00203                 nInfo = (FMSTR_SIZE) pte->info.n;
00204             else
00205                 nInfo = (FMSTR_SIZE) pte->info.p;
00206             
00207             /* variable entry only (also check read-write flag) */
00208             if((nInfo & FMSTR_TSA_INFO_VAR_FLAG) && (!bWriteAccess || (nInfo & FMSTR_TSA_INFO_RWV_FLAG)))
00209             {
00210                 /* need to take the larger of the two in union (will be optimized by compiler anyway) */
00211                 /*lint -e{506,774} condition always true/false */
00212                 if(sizeof(pte->addr.p) < sizeof(pte->addr.n))
00213                 {
00214                     if(FMSTR_CheckMemSpace(dwAddr, nSize, (FMSTR_ADDR) pte->addr.n, (FMSTR_SIZE) (nInfo >> 2)))
00215                     {
00216                         return FMSTR_TRUE; /* access granted! */
00217                     }
00218                 }
00219                 else
00220                 {
00221                     /*lint -e{923} casting pointer to long (on some architectures) */
00222                     if(FMSTR_CheckMemSpace(dwAddr, nSize, (FMSTR_ADDR) pte->addr.p, (FMSTR_SIZE) (nInfo >> 2)))
00223                     {
00224                         return FMSTR_TRUE; /* access granted! */
00225                     }
00226                 }
00227             }
00228             
00229             pte++;
00230         }
00231     }
00232 
00233     /* no more writable memory chunks available */
00234     if(bWriteAccess)
00235     {
00236         return FMSTR_FALSE;
00237     }
00238     
00239     /* allow reading of recorder buffer */
00240 #if FMSTR_USE_RECORDER
00241     if(FMSTR_IsInRecBuffer(dwAddr, nSize))
00242     {
00243         return FMSTR_TRUE;
00244     }
00245 #endif
00246 
00247     /* allow reading of any C-constant string referenced in TSA tables */
00248     for(nTableIndex=0U; (pte=FMSTR_TsaGetTable(nTableIndex, &cnt)) != NULL; nTableIndex++)
00249     {
00250         FMSTR_ADDR tmpAddr;
00251         
00252         /* allow reading of the TSA table itself */
00253         FMSTR_PTR2ADDR(tmpAddr, pte);
00254         if(FMSTR_CheckMemSpace(dwAddr, nSize, tmpAddr, cnt))
00255         {
00256             return FMSTR_TRUE;
00257         }
00258         
00259         /* number of items in a table */
00260         cnt /= (FMSTR_TSA_TSIZE) sizeof(FMSTR_TSA_ENTRY);
00261 
00262         /* all table entries */
00263         for(i=0U; i<cnt; i++)
00264         {
00265             /* system strings are always accessible at C-pointers */
00266             FMSTR_PTR2ADDR(tmpAddr, pte->name.p);
00267             if(pte->name.p)
00268             {
00269                 if(FMSTR_CheckMemSpace(dwAddr, nSize, tmpAddr, FMSTR_StrLen(tmpAddr)))
00270                 {
00271                     return FMSTR_TRUE;
00272                 }
00273             }
00274             
00275             FMSTR_PTR2ADDR(tmpAddr, pte->type.p);
00276             if(pte->type.p)
00277             {
00278                 if(FMSTR_CheckMemSpace(dwAddr, nSize, tmpAddr, FMSTR_StrLen(tmpAddr)))
00279                 {
00280                     return FMSTR_TRUE;
00281                 }
00282             }
00283             
00284             pte++;
00285         }
00286     }
00287 
00288     /* no valid TSA entry found => not-safe to access the memory */
00289     return FMSTR_FALSE;
00290 } 
00291 
00292 #else /* FMSTR_USE_TSA && (!FMSTR_DISABLE) */
00293 
00294 /*lint -efile(766, freemaster_protocol.h) include file is not used in this case */
00295 
00296 #endif /* FMSTR_USE_TSA && (!FMSTR_DISABLE) */
00297